<?php
namespace catchAdmin\apipatient\controller;

use catcher\exceptions\FailedException;
use MongoDB\BSON\ObjectId;
use think\exception\FileException;
use think\Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use app\common\model\Follow as FollowModel;
use catchAdmin\apipatient\validate\FollowValidate;
use think\exception\ValidateException;
use app\common\model\PushMessage as PushMessageModel;
use app\common\model\FollowHistory as FollowHistoryModel;

class Follow extends CatchController
{ 
    /**
     * 患者随访表单
     *
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_sf_list(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(FollowValidate::class)->scene('get_ed_list')->check($req);
            return CatchResponse::paginate((new FollowModel())->getPatientFollow(request()->userInfo['patientId'], $req['type']));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 消息列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_hzd_message_list(Request $request): \think\response\Json
    {
        #验证参数
        try {
            $where[] = ['category', '=', 'Patient'];
            $where[] = ['patientId', '=', request()->userInfo['patientId']];
            $list = (new PushMessageModel())->getLists($where);
            foreach ($list as $key => $val) {
                switch ($val['type']){
                    case 1:
                        $questionnaire = (new FollowHistoryModel())->where('id', $val['history_id'])
                            ->field('status,answer_id')
                            ->find();
                        $list[$key]['taskname'] = $val['projectName'];
                        $list[$key]['answer_id'] = $questionnaire['answer_id'];
                        $list[$key]['questStatus'] = $questionnaire['status'];
                        break;
                    case 2:
                        $list[$key]['taskname'] = $val['ed_name'];
                        break;
                    default:
                        break;
                }
            }
            return CatchResponse::paginate($list);
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 统计未读消息数
     * @param Request $request
     * @return \think\response\Json
     */
    public function getHzdUnreadMessageNum(Request $request): \think\response\Json
    {
        $where[] = ['category', '=', 'Patient'];
        $where[] = ['patientId', '=', request()->userInfo['patientId']];
        $where[] = ['isview', '=', 0];
        $num = (new PushMessageModel())->getCount($where);
        return CatchResponse::success(['unread' => $num]);
    }

    /**
     * 清除消息
     * @param Request $request
     * @return \think\response\Json
     */
    public function cleanHzdUnreadMessage(Request $request): \think\response\Json
    {
        $id = $request->param('id');
        if (!isset($id) || empty($id)) {
            throw new FileException("id不能为空");
        }
        return CatchResponse::success((new PushMessageModel())->delData($id));
    }

    /**
     * 阅读消息
     * @param Request $request
     * @return \think\response\Json
     */
    public function readHzdMessage(Request $request): \think\response\Json
    {
        $req = $request->param();

        if (!isset($req['id']) || empty($req['id'])) {
            throw new FailedException('id不能为空');
        }
        $data = ['isview' => 1];

        $msgInfo = (new PushMessageModel())->where('id', $req['id'])->find();

        //如果是分娩30天消息，更改随访状态
        if (isset($msgInfo['is_contact_follow']) == 1) {
            (new FollowModel())->saveData($msgInfo['follow_id'], ['followStatus' => 1]);
        }
        return CatchResponse::success((new PushMessageModel())->saveData($req['id'], $data));
    }

    /**
     * 随访消息
     * @param Request $request
     * @return \think\response\Json
     */
    public function followMessage(Request $request): \think\response\Json
    {
        $where[] = ['category', '=', 'Patient'];
        $where[] = ['patientId', '=', request()->userInfo['patientId']];
        $where[] = ['is_contact_follow', '=', 1];
        $list = (new PushMessageModel())->getLists($where);
        return CatchResponse::paginate($list);
    }
}